package com.cat.jbpm.def;

import java.util.List;

import org.jbpm.api.model.OpenExecution;
import org.jbpm.api.task.Assignable;
import org.jbpm.api.task.AssignmentHandler;
import org.jbpm.pvm.internal.env.EnvironmentImpl;

import com.cat.entity.TbUser;
import com.cat.service.common.UserService;
import com.cat.service.common.impl.UserServiceImpl;

public class UserAssignHandler implements AssignmentHandler {

	private static final long serialVersionUID = 263802333322348808L;

	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	private String roleIds;

	@Override
	public void assign(Assignable assignable, OpenExecution execution)
			throws Exception {
		// String name =(String) execution.getVariable("name");
		// System.out.println(name);
		System.out.println(roleIds);
		userService = EnvironmentImpl.getFromCurrent(UserServiceImpl.class);
		System.out
				.println(execution.getProcessDefinitionId() + "角色：" + roleIds);
		if (roleIds != null) {
			List<TbUser> users = userService.getUserListByRoleId(roleIds);
			// String assignes="";
			if (users != null) {
				for (TbUser user : users) {
					assignable.addCandidateUser(user.getAccountNo());
				}
			} else {
				assignable.setAssignee("admin");
			}
		} else {
			assignable.setAssignee("admin");
		}

	}
}